package com.wx.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.wx.kit.MessageUtil;
import com.wx.message.response.TextMessage;

@RestController
public class PortalController {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	
	@GetMapping(path = "/portal")
	public void portal(HttpServletRequest req, HttpServletResponse resp) {
		String signature = req.getParameter("signature");
		String timestamp = req.getParameter("timestamp");
		String nonce = req.getParameter("nonce");
		String echostr = req.getParameter("echostr");
		System.out.println(signature);
		System.out.println(timestamp);
		System.out.println(nonce);
		PrintWriter out;
		try {
			out = resp.getWriter();
			out.print(echostr);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@ResponseBody
	@PostMapping(path = "/portal")
	public String portal(HttpServletRequest request) {
		// 响应的XML串
		String respXml = "";
		// 要响应的文本内容
		String respContent = "未知的消息类型";

		try {
			Map<String, String> requestMap = MessageUtil.parseXml(request);
			logger.info(requestMap.toString());
			logger.warn(requestMap.toString());
			// 行为发起人的openid
			String fromUserName = requestMap.get("FromUserName");
			// 行为接受人
			String toUserName = requestMap.get("ToUserName");
			String MsgType = requestMap.get("MsgType");
			String Content = requestMap.get("Content");
			String Event = requestMap.get("Event");
			// 客户关注以及取消关注行为
			if (MsgType.equals(MessageUtil.RESP_EVENT_TYPE_TEXT)) {
				if (Event.equals(MessageUtil.RESP_SUBSCRIBE_TEXT)) {
					// TODO 注入用户关注信息到数据库
					System.out.println("用户关注该公众号");
				} else if (Event.equals(MessageUtil.RESP_UNSUBSCRIBE__TEXT)) {
					// TODO 删除数据库的用户关注信息
					System.out.println("用户取消关注该公众号");
				} else if (Event.equals(MessageUtil.TEMPLATE_SEND_JOB_FINISH)) {
					// TODO 记录消息推送成功的相关操作
					System.out.println("推送消息成功");
				}
			}
			// 客户发送文本消息
			if (MsgType.equals(MessageUtil.RESP_MESSAGE_TYPE_TEXT)) {
				respContent = "岳扬的公众号收到了您的一条文本消息：" + Content + "，时间戳是：" + (new Date().getTime());
			}
			// 构建一条文本消息
			TextMessage textMessage = new TextMessage();
			textMessage.setToUserName(fromUserName);
			textMessage.setFromUserName(toUserName);
			textMessage.setCreateTime(new Date().getTime());
			textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
			textMessage.setContent(respContent);
			respXml = MessageUtil.messageToXml(textMessage);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return respXml;
	}
}
